﻿''' <summary>
''' FileUploadHandler handles the async upload for SilverLight based File Uploader
''' </summary>
''' <remarks>
''' </remarks>
Public Class FileUploadHandler
    Implements IHttpHandler
    Implements SessionState.IRequiresSessionState

    Private _context As HttpContext
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        _context = context
        Dim uploadPath As String
        If String.IsNullOrEmpty(_context.Request.QueryString("path")) Then
            uploadPath = context.Server.MapPath("~/temp")
        Else
            uploadPath = context.Server.MapPath("~/" & _context.Request.QueryString("path"))
        End If
        Dim fileUpload As New FileUploadProcess()
        AddHandler fileUpload.FileUploadCompleted, AddressOf fileUpload_FileUploadCompleted
        'fileUpload.FileUploadCompleted += New FileUploadCompletedEvent()
        fileUpload.ProcessRequest(context, uploadPath)
    End Sub
    Sub fileUpload_FileUploadCompleted(ByVal sender As Object, ByVal args As FileUploadCompletedEventArgs)
        Dim id As String = _context.Request.QueryString("id")
        _context.Session("filename_Ext") = args.FileName
        _context.Session("FilePath") = args.FilePath
        _context.Session("filename") = IO.Path.GetFileNameWithoutExtension(args.FileName)
        _context.Session("Ext") = IO.Path.GetExtension(args.FileName)

        'FileInfo fi = new FileInfo(args.FilePath);
        'string targetFile = Path.Combine(fi.Directory.FullName, args.FileName);
        'if (File.Exists(targetFile))
        '    File.Delete(targetFile);
        'fi.MoveTo(targetFile);
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class